package com.songwo.util;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
//        String code = "061ncFvP1eQwQ21bRnwP1TBFvP1ncFvG";
//        String s = HttpClientUtils.sendGet("https://api.weixin.qq.com/sns/jscode2session?" +
//                "appid=wx52140f7496462066&" +
//                "secret=421b7ba441424d11d009dab88b3af479&" +
//                "js_code=" + code + "&" +
//                "grant_type=authorization_code");
//        System.out.println(s);

//        {"session_key":"eyem9aDCrGAaOVUYooIR3g==","openid":"o4W6o5XBzfSoHioVgE5fo3F7DwUg"}

        // 返回信息
        // {"session_key":"rMYBfkQjrGgIstUj\/J9YHA==","openid":"oN0_50Bd2fS6e6QzdVUvmzCw0INo"}
//        {"session_key":"am\/FDjW7wKG7pM02dqRDVA==","openid":"oN0_50Bd2fS6e6QzdVUvmzCw0INo"}

        String encryptedData = "gT+8KaA3JEwx32djEPOlVKbi4j8n1uaYcA7TFgwHOmQCCqsif0QSxjhdzqv/Lc5vWaO2Fq/is8GCShjS9vjBxCLUHD6HXN0ELrgNEEKoo/v1BwdfBTjSy/CvzWpG+/6TAwcj5kHBue/8Qzmhxbqfy8roQ1FwyzNoFhBFW8uRXWqkEgQ3xwZZ+7j91HtjISflNwi2ud80bh/RFOgMTYwgsQ==";
        String sessionKey = "eyem9aDCrGAaOVUYooIR3g==";
        String iv = "Jd5dHouLF0v3pZVysHNrGg==";
        Map<String,Object> map = new HashMap<>(5);
        try {
            // AES 为解密工具类
            byte[] resultByte  = AES.decrypt(Base64.decodeBase64(encryptedData),
                    Base64.decodeBase64(sessionKey),
                    Base64.decodeBase64(iv));
            if( null != resultByte && resultByte.length > 0){
                String userInfo = new String(resultByte, "UTF-8");
                map.put("status", "1");
                map.put("msg", "解密成功");
                map.put("userInfo", userInfo);
            }else{
                map.put("status", "0");
                map.put("msg", "解密失败");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

//        String decodeJSON = JSON.toJSONString(map);
//        System.out.println(decodeJSON);

//        String res;
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        long lt = new Long("1520524800");
//        Date date = new Date(lt);
//        res = simpleDateFormat.format(date);
//        System.out.println(res);
    }
}
